package com.ljt.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * 权限认证配置类
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private CaptchaFilter captchaFilter;
    @Autowired
    private JWTLogoutSuccessHandler jwtLogoutSuccessHandler;

    @Autowired
    private JWTAuthenticationEntryPoint jwtAuthenticationEntryPoint;

    @Autowired
    private JWTAccessDeniedHandler jwtAccessDeniedHandler;

    @Autowired
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;

    @Autowired
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    JWTAuthenticationFilter jwtAuthenticationFilter() throws Exception {
        return new JWTAuthenticationFilter(authenticationManager());
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        String[] strings = new String[]{
                "/captcha",
                "/login",
                "/productCategory/getProductCategoryMenuTree",
                "/product/getProductWithCategoryName",
                "/product/queryAllProductByCondition",
                "/user/queryAllUserByPage",
                "/user/queryAllUserByUserName",
                "/user/queryUserById",
                "/user/insertUser",
                "/user/updateUserById",
                "/user/updateUserById2",
                "/user/deleteUserById",
                "/user/exportUserToExcel",
                "/user/importUserToExcel",
                "/department/queryDeptHasCount",
                "/department/insertDept",
                "/department/updateDeptById",
                "/department/deleteDeptById",
                "/department/exportDepartmentToExcel",
                "/department/importInStockToExcel",
                "/department/queryAllDeptByCondition",
                "/department/queryDeptById",
                "/system/role/queryAllRoleByPage",
                "/system/role/deleteRoleById",
                "/system/role/updateRoleById",
                "/system/role/updateRoleById2",
                "/system/role/insertRole",
                "/system/role/queryByRoleName",
                "/system/role/exportRoleToExcel",
                "/system/role/importRoleToExcel",
                "/health/queryHealthCount",
                "/health/saveHealth",
                "/health/deleteHealth",
                "/health/updateHealth",
                "/business/consumer/queryConsumerCount",
                "/business/consumer/saveConsumer",
                "/business/consumer/deleteConsumer",
                "/business/consumer/updateConsumer",
                "/applyFor/queryAllApplyForByCondition",
                "/applyFor/insertApplyFor",
                "/business/consumer/queryConsumerCount",

        };
        // 关闭跨域攻击
        http.cors().and().csrf().disable()

                // 登录配置
                .formLogin()
                .failureHandler(myAuthenticationFailureHandler)
                .successHandler(myAuthenticationSuccessHandler)

                // 登出配置
                .and()
                .logout()
                .logoutSuccessHandler(jwtLogoutSuccessHandler)

                // 关闭session
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)

                // 配置拦截规则
                .and()
                .authorizeRequests()
                .antMatchers(strings)
                .permitAll()
                .anyRequest().authenticated()

                // 异常处理器
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(jwtAuthenticationEntryPoint)
                .accessDeniedHandler(jwtAccessDeniedHandler)

                // 自定义过滤器
                .and()
                .addFilter(jwtAuthenticationFilter())
                .addFilterBefore(captchaFilter, UsernamePasswordAuthenticationFilter.class)
        ;
    }
}
